home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / os2 / adaptor.zip / ADAPT.ZIP / adaptor / src / xhelp.c < prev    next >
C/C++ Source or Header  |  1993-07-08  |  15KB  |  508 lines

  1. /**************************************************************************
  2. *                                                                         *
  3. *  Author      : Werner Meurer, GMD, I1.HR                                *
  4. *  Date        : Jan 93                                                   *
  5. *  Last Update :                                                          *
  6. *                                                                         *
  7. *  Module      : xhelp                                                    *
  8. *                                                                         *
  9. *  Function    : Athena Widgets for Help Handling                         *
  10. *                                                                         *
  11. *  Export :                                                               *
  12. *                                                                         *
  13. *    void make_help_available (Widget w)                                  *
  14. *                                                                         *
  15. *       - gets help entry for the widget based on its label               *
  16. *                                                                         *
  17. *    void make_help_menu_entries (Widget father)                          *
  18. *                                                                         *
  19. **************************************************************************/
  20.  
  21. #define DEBUG(x) 
  22.  
  23. #define MAXHELP    40          /* maximal help records */
  24.  
  25. #define MAXHELPNAMELENGTH    30     /* maximal length of a help name */
  26.  
  27. #define PIXELS    15          /* used to compute size of help widget */
  28.  
  29. #define EOL     10          /* character for end of line */
  30.  
  31. #include <string.h>
  32.  
  33. #include "xglobal.h"
  34. #include "xfiles.h"
  35.  
  36. /*********************************************************************
  37. *                                                                    *
  38. *  Global Data is Array of Entries                                   *
  39. *                                                                    *
  40. *********************************************************************/
  41.  
  42. static char HELPFILE[165];
  43.  
  44. struct HELP{
  45.     char    HelpName[MAXHELPNAMELENGTH];
  46.     long    start,
  47.         end;
  48.     Widget    window;
  49. };
  50.  
  51. static struct HELP help[MAXHELP+1];
  52.  
  53. static int helpcount = 0;    /* Number of entries in help */
  54.  
  55. void print_help_entries ();
  56.  
  57. /****************************************************************
  58. *                                         *
  59. *   helpinit()                            *
  60. *   INPUT:    none                        *
  61. *   OUTPUT:    none                        *
  62. *   Initialising the Arraystruktur help with 0!            *
  63. *   Initialising the Directory with the file help!        *
  64. *                                       *
  65. ****************************************************************/
  66.  
  67. helpinit()
  68. {
  69.     int i,x;
  70. DEBUG(fprintf(stdout,"helpinit()<-\n"));
  71.     for (i=0;i<MAXHELP;i++)
  72.     {
  73.       for (x=0;x<MAXHELPNAMELENGTH;x++)
  74.         help[i].HelpName[x] = '\0';
  75.       help[i].start = 0;
  76.       help[i].end = 0;
  77.       help[i].window = 0;
  78.     }
  79.     strcpy(HELPFILE,PHOME);
  80.     strcpy(HELPFILE+strlen(HELPFILE),"/help");
  81. DEBUG(fprintf(stdout,"helpinit()->\n"));
  82. }
  83.  
  84. /****************************************************************/
  85. /* get_help_entries()                        */
  86. /* INPUT:    none                        */
  87. /* OUTPUT:    none                        */
  88. /* Loads Data in the Struktur help.                */
  89. /****************************************************************/
  90.  
  91. void get_help_entries ()
  92. {
  93.     FILE *helpfileptr = 0;
  94.     char letter;
  95.     char command[MAXHELPNAMELENGTH];
  96.     int commandptr;
  97.     long start,end;
  98. DEBUG(fprintf(stdout,"get_help_entries()<-\n"));
  99.     helpinit();
  100.     helpfileptr = fopen(HELPFILE,"r");
  101. DEBUG(fprintf(stdout,"HELPFILE = %s\nhelpfileptr = %d\n",
  102.     HELPFILE,(unsigned long)helpfileptr));
  103.     if (!helpfileptr)
  104.     {
  105.         strcpy(last_message,"HELPFILE wasn't found or no access\n");
  106.         set_message();
  107.         fprintf(stderr,"HELPFILE wasn't found or no access\n");
  108.         return;
  109.     }
  110.     letter= getc(helpfileptr);/* 1. charakter of command */
  111.     while (letter!= EOF)
  112.     {
  113.         if (letter== '.')
  114.         /* command */
  115.         {
  116.             if (help[helpcount].start)
  117.             {
  118.                 help[helpcount].end = ftell(helpfileptr) -2;
  119. DEBUG(fprintf(stdout,"end = %d\n",help[helpcount].end));
  120.             }
  121.             commandptr = 0;
  122.             letter = getc(helpfileptr);
  123.             while ((letter != EOF) &&
  124.                 (letter != '.') && 
  125.                 (letter != EOL))
  126.             {
  127.                 if (commandptr < MAXHELPNAMELENGTH)
  128.                 {
  129.                    command[commandptr] = letter;
  130.                    commandptr++;
  131.                    command[commandptr] = '\0';
  132.                 }
  133.                 letter= getc(helpfileptr);
  134.             }
  135.             letter = getc(helpfileptr);
  136. DEBUG(fprintf(stdout,":%s:\t%d\n",help[helpcount].HelpName,helpcount));
  137.                         if (helpcount == MAXHELP)
  138.                         {
  139.                             fprintf(stderr,"maximal number of helpentries has benn reached (%d)\n",MAXHELP);
  140.                             return;
  141.                         }
  142.             if (help[helpcount].HelpName[0] == '\0')
  143.                 strncpy(help[helpcount].HelpName,command,
  144.                     MAXHELPNAMELENGTH);
  145.             else
  146.                 strncpy(help[helpcount+1].HelpName,command,
  147.                     MAXHELPNAMELENGTH);
  148. DEBUG(fprintf(stdout,"command = %s\thelpcount = %d\n",
  149. help[helpcount].HelpName,helpcount));
  150.         }
  151.         else
  152.         /* help */
  153.         {
  154.             if ((helpcount == 0) || (help[helpcount].end))
  155.             {
  156.                 if ((help[helpcount].start) && 
  157.                     (help[helpcount].end))
  158.                 {
  159.                    helpcount++;
  160.                    help[helpcount].start = ftell(helpfileptr)-1;
  161.                 }
  162.                 if ((!help[helpcount].start) &&
  163.                     (!help[helpcount].end))
  164.                 {
  165.                    help[helpcount].start = ftell(helpfileptr)-1;
  166.                 }
  167. DEBUG(fprintf(stdout,"start = %d\n",help[helpcount].start));
  168.             }
  169.             while ((letter!= EOF) && (letter != EOL))
  170.             {
  171.                 letter = getc(helpfileptr);
  172.             }
  173.             if (letter == EOL)
  174.             {
  175.                 letter = getc(helpfileptr);
  176.             }
  177.             if (letter == EOF)
  178.             {
  179.                 help[helpcount].end = ftell(helpfileptr) -2;
  180. DEBUG(fprintf(stdout,"end = %d\n",help[helpcount].end));
  181.                 helpcount++;
  182.             }
  183.         }
  184.     }
  185.     fclose(helpfileptr);
  186. DEBUG(fprintf(stdout,"get_help_entries()->\n"));
  187. }
  188.  
  189. /****************************************************************/
  190. /* HelpSelect()                            */
  191. /* INPUT:    w is the calling Widget                */
  192. /*        client_data are the calling Parameters        */
  193. /*        call_data                     */
  194. /* OUTPUT:    The called help is printed             */
  195. /****************************************************************/
  196.  
  197. static void HelpSelect (w, client_data, call_data)
  198. Widget w;
  199. XtPointer client_data;
  200. XtPointer call_data;
  201. {
  202.   FILE *helpfileptr;
  203.   int nr;
  204.   int rows = 3;
  205.   int maxline = 0,line = 0;
  206.   long stelle;
  207.   char *string;
  208.   Arg args[2];
  209.   int x,y;
  210.  
  211.   DEBUG(fprintf (stdout, "one help entry selected\n"));
  212.   print_help_entries();
  213.  
  214.   nr = (int) client_data;
  215.   if (helpcount == nr)
  216.   {
  217.     Arg arg;
  218.     char *label;
  219.     string = "No help available!\n";
  220.     rows++;
  221.     XtSetArg(arg,XtNlabel,&label);
  222.     XtGetValues(w,&arg,1);
  223.       xshowhelp(w,label,string,rows,strlen(string));
  224.     return;
  225.   }
  226.   DEBUG(printf("Help entry %d for command %s selected\n",nr,help[nr].HelpName));
  227.   helpfileptr = fopen(HELPFILE,"r");
  228.   if (!helpfileptr)
  229.   {
  230.     fprintf(stderr,"No access on HELPFILE\n");
  231.     return;
  232.   }
  233.   DEBUG(fprintf(stdout,"start = %d\tend = %d\n",help[nr].start,help[nr].end));
  234.   if (fseek(helpfileptr,help[nr].start,(long) 0) != 0)
  235.   {
  236.     fprintf(stderr,"seek failed \n");
  237.     return;
  238.   }
  239.   string = (char*) malloc(help[nr].end - help[nr].start +2);
  240.   if (!string)
  241.   {
  242.     fprintf(stderr,"string not allocated\n");
  243.     return;
  244.   }
  245.   for (stelle = help[nr].start;stelle <= help[nr].end;stelle++)
  246.   {
  247.     string[stelle-help[nr].start] = getc(helpfileptr);
  248.     if (string[stelle-help[nr].start] == EOL)
  249.     {
  250.         rows++;
  251.         if (line > maxline) maxline = line;
  252.         line = 0;
  253.     }
  254.     else
  255.     {
  256.         line++;
  257.     }
  258.     DEBUG(fprintf(stdout,"%c",string[stelle-help[nr].start]));
  259.   }
  260.     /* only one line of characters is shown in message window */
  261.   if (rows == 4)
  262.   {
  263.     string[help[nr].end - help[nr].start] = '\0';
  264.         /* not end - start + 1 to remove EOL */
  265.     strcpy(last_message,string);
  266.     set_message();
  267.   }
  268.   else /* more lines are shown in help window */
  269.   {
  270.         string[help[nr].end - help[nr].start + 1] = '\0'; /* terminate string */
  271.  
  272.     DEBUG(fprintf(stdout,"HelpName = %s\n",help[nr].HelpName));
  273.  
  274.     xshowhelp(w,help[nr].HelpName,string,rows,maxline);
  275.   }
  276.   free(string);
  277.   if (fclose(helpfileptr))
  278.   {
  279.     fprintf(stderr,"fclose failed\n");
  280.     return;
  281.   }
  282. }
  283.  
  284. /****************************************************************/
  285. /* make_help_menu_entries()                    */
  286. /* INPUT:    fatherwidget                    */
  287. /* OUTPUT:                            */
  288. /* return value:    true if helpcount is greater than 0    */
  289. /*            false, if helpcount is 0        */
  290. /* Making the pulldownmenu                    */
  291. /****************************************************************/
  292. bool make_help_menu_entries (father)
  293.  
  294. Widget father;
  295.  
  296. { int i;
  297.   Widget entry, menu;
  298.   char *item;
  299.  
  300.   get_help_entries ();
  301.  
  302.   menu = XtCreatePopupShell("menu", simpleMenuWidgetClass,
  303.                             father, NULL, 0);
  304.  
  305.   for (i=0; ((i < helpcount) && (helpcount < MAXHELP)); i++)
  306.     { item = help[i].HelpName;
  307.       entry = XtCreateManagedWidget (item, smeBSBObjectClass, menu, NULL, 0);
  308.       XtAddCallback (entry, XtNcallback, HelpSelect, (XtPointer) i);
  309.     }
  310.  
  311.   DEBUG(fprintf (stdout, "help entries created"));
  312.   print_help_entries();
  313.  
  314.   if (helpcount)
  315.     return(true);
  316.   else
  317.     return(false);
  318. }
  319.  
  320. void show_help_quit (w, client_data, call_data)
  321. Widget w;
  322. XtPointer client_data, call_data;
  323. { Widget parent;
  324.   int i;
  325.   parent = XtParent (XtParent (w));
  326.   for (i=0;i<helpcount;i++)
  327.   {
  328.     if (help[i].window == w)
  329.     {
  330.         help[i].window = 0;
  331.         XtPopdown (parent);
  332.         XtDestroyWidget (parent);
  333.         return;
  334.     }
  335.   }
  336. }
  337.  
  338. xshowhelp (w,name, string, rows, maxline)
  339. Widget w;
  340. char name[];
  341. char *string;
  342. int rows;
  343. int maxline;
  344. {  int n;
  345.    Arg args[10];
  346.  
  347.    Widget sh_popup, sh_form, sh_quit,sh_text;
  348.  
  349.    int i;
  350.    Widget parent = XtParent(w);
  351.    /* Get the positions to set the position of the helpwindow. */
  352.    /* It should be shown under the button which asks for help. */
  353.    Position x1,x2,y;
  354.  
  355.    n = 0;
  356.    XtSetArg(args[n],XtNx,&x1);n++;
  357.    XtSetArg(args[n],XtNy,&y);n++;
  358.    XtGetValues(XtParent(XtParent(parent)),args,n);
  359.    n = 0;
  360.    XtSetArg(args[n],XtNx,&x2);n++;
  361.    XtGetValues(w,args,n);
  362.    for (i=0;((i<helpcount) && (strcmp(name,help[i].HelpName) != 0)) ;i++)
  363.     ;
  364.    if (helpcount == i)
  365.     /* help not available */
  366.    {
  367.     strcpy(last_message,
  368.         "No help available ");
  369.     set_message();
  370.     return;
  371.    }
  372.    if (help[i].window != 0)
  373.     /* helpwindow is open */
  374.    {
  375.     strcpy(last_message,
  376.         "helpwindow is open");
  377.     set_message();
  378.     return;
  379.    }
  380.    sh_popup = XtCreatePopupShell (
  381.          name,
  382.          transientShellWidgetClass,
  383.          parent,
  384.          NULL, 0);
  385.  
  386.    /* Set the position of the helpwindow */
  387.    n = 0;
  388.    XtSetArg(args[n],XtNx,x1+x2);n++;
  389.    XtSetArg(args[n],XtNy,y+100);n++;
  390.    XtSetValues(sh_popup,args,n);
  391.  
  392. /****************************************************************************
  393. *                                                                           *
  394. *  formWidget                                                               *
  395. *                                                                           *
  396. *  sh_form :  sh_quit                                                       *
  397. *             sh_text                                                       *
  398. *                                                                           *
  399. ****************************************************************************/
  400.  
  401.       sh_form = XtCreateManagedWidget(
  402.                   "sh_form",                    /* widget name */
  403.                   formWidgetClass,              /* widget class */
  404.                   sh_popup,                     /* parent widget*/
  405.                   NULL, 0);                     /* terminate varargs list */
  406.                 
  407.       sh_quit = XtCreateManagedWidget(
  408.                   "Quit",                       /* widget name */
  409.                   commandWidgetClass,           /* widget class */
  410.                   sh_form,                      /* parent widget*/
  411.                   NULL, 0);                     /* terminate varargs list */
  412.  
  413.       help[i].window = sh_quit;
  414.       
  415.       XtAddCallback(sh_quit, XtNcallback, show_help_quit, 0);
  416.                 
  417.       n = 0;
  418.       XtSetArg (args[n], XtNhorizDistance, 10);  n++;
  419.       XtSetArg (args[n], XtNfromHoriz, sh_quit);  n++;
  420.  
  421.                 
  422.       n = 0;
  423.       if (rows > 20) rows = 20;
  424.       XtSetArg (args[n], XtNheight,
  425.     (int) ((float)(PIXELS*rows))); n++; 
  426.       {
  427.     if (maxline > 80) maxline = 80;
  428.       XtSetArg (args[n], XtNwidth, (int)(0.5 * (float) (PIXELS*maxline))); n++;
  429.       }
  430.       XtSetArg (args[n], XtNvertDistance, 10);  n++;
  431.       XtSetArg (args[n], XtNfromVert, sh_quit);  n++;
  432.       XtSetArg (args[n], XtNtype, XawAsciiString);  n++;
  433.       XtSetArg (args[n], XtNstring, string);  n++;
  434.       XtSetArg (args[n], XtNeditType, XawtextRead);  n++;
  435.       XtSetArg (args[n], XtNscrollVertical, 1);  n++;
  436.       XtSetArg (args[n], XtNscrollHorizontal, 1);  n++; 
  437.  
  438.       sh_text = XtCreateManagedWidget (
  439.                  "sh_text",
  440.                  asciiTextWidgetClass,
  441.                  sh_form,
  442.                  args, n);  
  443.  
  444.       XtPopup (sh_popup, XtGrabNone);
  445.  
  446. }
  447.  
  448. void print_help_entries ()
  449. {
  450. DEBUG( int i);
  451. DEBUG(fprintf(stdout,"There are %d help entries\n", helpcount));
  452. DEBUG(for(i=0;i<helpcount;i++))
  453. DEBUG(printf("HelpName %s length = %d\n",
  454.     help[i].HelpName,strlen(help[i].HelpName)));
  455. }
  456.  
  457. /****************************************************************
  458. *                                         *
  459. *  show_help()                            *
  460. *                                         *
  461. *  INPUT:    w is the calling widget                *
  462. *          event is the event which called this procedure    *
  463. *  OUTPUT:    none                        *
  464. *  This procedure compares the label of the calling widget with    *
  465. *  the HelpNames. If it was equal, it calles the procedure    *
  466. *  SelectHelp, which shows the helptext.            *
  467. ****************************************************************/
  468.  
  469. void show_help (w, event)
  470. Widget w;
  471. XEvent *event;
  472. {
  473.     char *widget_name;
  474.     int i;
  475.  
  476.         widget_name = XtName (w);
  477.  
  478.     DEBUG(fprintf(stdout,"label = %s\tlength = %d\n",widget_name,strlen(widget_name)));
  479.     for (i=0;
  480.         ((i<helpcount) && (strcmp(help[i].HelpName,widget_name) != 0));
  481.         i++)
  482.     {
  483.         ;    
  484.     }
  485.  
  486.         if (i == helpcount)
  487.            { /* no help availble for this widget */
  488.              sprintf (last_message, "No Help available for '%s'", widget_name);
  489.              set_message ();
  490.            }
  491.          else
  492.            {
  493.              DEBUG(fprintf(stdout,"HelpSelect is called with %d\n",i));
  494.              HelpSelect (w, (XtPointer) i, (XtPointer) 0);
  495.            }
  496. }
  497.  
  498.  
  499. static String helpTranslation =
  500.         "<Btn3Down>:    show_help()";
  501.  
  502. void make_help_available (w)
  503.  
  504. Widget w;
  505.  
  506. { XtOverrideTranslations(w, XtParseTranslationTable(helpTranslation));
  507. }
  508.